from pgzrun import *
from facedetect import *
import random
import os
music.set_volume(0.2)
music.play("yinyue.mp3")

WIDTH = 1200
HEIGHT = 600
al = []
ci = 0
d_list = []
hhh = []
feiji=Actor("feiji.png",(-600,-550))
diji=Actor("diji.png",(-600,-500))
zidan=Actor("zidan.png",(-600,-500))
win=Actor("win.png",(-600,-500))
shibai=Actor("shibai.png",(-600,-500))
it=Actor("it.png",(1140,100))
ceshi=Actor("ceshi.png",(600,200))
kaishi=Actor("kaishi.png",(600,300))
wenzi=Actor("wenzi.png",(-999,-999))
tuichu=Actor("tuichu.png",(-999,-999))
youxiu=Actor("优秀.png",(-999,-999))
lianghao=Actor("良好.png",(-999,-999))
hege=Actor("合格.png",(-999,-999))
buhege=Actor("不合格.png",(-999,-999))
biaoti=Actor("标题.png",(600,50))
phb=Actor("排行榜.png",(1140,230))
# 初始化摄像头人脸检测
cf = CameraFace()
a = 0
b = 1
c = 0
d1 = 0
d2 = 0
d3 = 0
d4 = 0
d5 = 0
d6 = 0
e = 0
f = 0
g = 0
h = 0
i = 0
j = 0
k = 0
l = 0
m = 0
n = 0
o = 0
def draw():
    global c,i,e,j,g,al,di
    screen.clear()
    screen.fill("white")
    zidan.draw()
    feiji.draw()
    diji.draw()
    win.draw()
    shibai.draw()
    it.draw()
    ceshi.draw()
    kaishi.draw()
    wenzi.draw()
    tuichu.draw()
    youxiu.draw()
    lianghao.draw()
    hege.draw()
    buhege.draw()
    biaoti.draw()
    phb.draw()
    if ci != 0:
        for draw in range(ci):
            d_list[draw].draw()
    if e == 1:
        screen.draw.text(f"得分：{c}/10分",(1000,5),color="red",gcolor="yellow",fontname="zh", fontsize=30)
        screen.draw.text(f"时间/秒：{int((time.time()-m)*1000)/1000}",(10,40),color="red",gcolor="yellow",fontname="zh", fontsize=30)
    if e == 1 or g == 1:
        screen.draw.text(f"子弹剩余/颗：{i}",(10,3),color="red",gcolor="yellow",fontname="zh", fontsize=30) 
    if n == 1:
        screen.draw.text("排行榜",(500,5),color="red",gcolor="yellow",fontname="zh", fontsize=70)
        al.sort()
        for xianshi in range(ci):
            screen.draw.text(f"第{xianshi+1}名：                             用时：{al[xianshi]}",(300,xianshi*100+100),color="red",gcolor="yellow",fontname="zh", fontsize=30)
def on_mouse_down(pos):
    global e,g,n,di,ci,hhh,al,d_list
    if kaishi.collidepoint(pos):
        feiji.pos = (600,550)
        diji.pos = (random.randint(100,1100),0)
        it.pos = (-600,-500)
        ceshi.pos = (-600,-500)
        kaishi.pos = (-600,-500)
        biaoti.pos = (-999,-999)
        phb.pos = (-999,-999)
        e = 1
    if it.collidepoint(pos):
        wenzi.pos = (600,300)
        tuichu.pos = (100,500)
        it.pos = (-600,-500)
        ceshi.pos = (-600,-500)
        kaishi.pos = (-600,-500)
        biaoti.pos = (-999,-999)
        phb.pos = (-999,-999)
    if tuichu.collidepoint(pos):
        if ci != 0:
            for tui in range(ci):
                d_list[tui].pos = (-999,-999) 
        wenzi.pos = (-999,-999)
        tuichu.pos = (-999,-999)
        kaishi.pos = (600,300)
        ceshi.pos = (600,200)
        it.pos = (1140,100)
        biaoti.pos = (600,50)
        phb.pos = (1140,230)
        n = 0
    if ceshi.collidepoint(pos):
        feiji.pos = (600,550)
        diji.pos = (random.randint(100,1100),0)
        it.pos = (-600,-500)
        ceshi.pos = (-600,-500)
        kaishi.pos = (-600,-500)
        biaoti.pos = (-999,-999)
        phb.pos = (-999,-999)
        g = 1
    if phb.collidepoint(pos):
        if os.path.exists("paihang.txt") == True:
            with open('paihang.txt', 'r') as file:
                lines = file.readlines()
                ci = len(lines)
                for line in lines:
                    hhh.append(line.replace("\n", ""))
                    al.append(float(line.split("_")[1]))
            hhh = sorted(hhh, key=lambda x: float(x.split("_")[1]))
            d_list = []
            for actor in range(ci):
                d_list.append(Actor(f"{hhh[actor]}.png",(-999,-999)))
        else:
            xinjian = open("paihang.txt", "w")
            xinjian.close()
        if ci != 0:
            for yi in range(ci):
                d_list[yi].pos = (430,yi*100+120)
        it.pos = (-600,-500)
        ceshi.pos = (-600,-500)
        kaishi.pos = (-600,-500)
        biaoti.pos = (-999,-999)
        phb.pos = (-999,-999)
        tuichu.pos = (100,500)
        n = 1
        
#框架
def zhu():
    global a,b,c,d1,d2,e,f,g,h,i,d3,d4,j,m,al
    #装弹
    if cf.has_shaked():
        if i < 3:
            sounds.shangtang.play()
        i += 3
        if i > 3:
            i = 3
    #飞机移动
    x, y = cf.get_face_pos()
    if x > 0:
        feiji.pos = (WIDTH * x,550)
    #子弹发射
    if i > 0:
        if cf.has_noded() and zidan.y < 0:
            sounds.fashe.play()
            a = 1
            zidan.pos = (feiji.x,feiji.y)
            i -= 1
            if zidan.y <= 0:
                zidan.pos = (-600,-500)
                a = 0
    if a == 1:
        zidan.y -= 10
    #碰到边缘就反弹
    if feiji.x >= 1200:
        feiji.x = 1200
    if feiji.x <= 0:
        feiji.x = 0
    if diji.x >= 1150:
        f = 1
    if diji.x <= 50:
        f = 0
    #敌机下降
    if f == 1:
        diji.x -= 5
    else:
        diji.x += 5
    diji.y += 1
#游戏主体
def update():
    cf.update(show_image=True)
    global a,b,c,d1,d2,e,f,g,h,i,d3,d4,j,m,o,di
    #游戏
    if e == 1:
        if b == 1:
            zhu()
            if m == 0:
                m = time.time()
            #爆炸、计时开始
            if zidan.colliderect(diji):
                sounds.baozha.play()
                diji.image = "baozha.png"
                d1 = time.time()
                b = 0
                c += 1
                zidan.pos = (-600,-500)
        #程序重启、计时结束
        elif time.time()-d1 >= 1:
            diji.image = "diji.png"
            diji.pos = (random.randint(100,1100),0)
            b = 1
        #胜利
        if c == 10:
            music.pause()
            win.pos = (600,300)
            sounds.shengli.play()
            e = 0
            j = 1
            d4 = time.time()
            # 胜利后，保存人脸到某个图片文件
            o = int(time.time())
            p = str(o)+"_"+str(int((time.time()-m)*1000)/1000)
            cf.save_face_to_file(f'images/{p}.png')
            with open('paihang.txt', 'a', encoding='utf-8') as file:
                file.write(p+"\n")
            m = 0
            
        #失败
        if diji.y > 550:
            music.pause()
            shibai.pos = (600,300)
            sounds.shibai.play()
            e = 0
            j = 1
            d4 = time.time()
    #返回主页面
    if time.time()-d4 >= 3 and j == 1:
        diji.image = "diji.png"
        kaishi.pos = (600,300)
        ceshi.pos = (600,200)
        it.pos = (1140,100)
        feiji.pos = (-999,999)
        diji.pos = (-999,-999)
        zidan.pos = (-600,-500)
        win.pos = (-999,-999)
        biaoti.pos = (600,50)
        shibai.pos = (-999,-999)
        phb.pos = (1140,230)
        j = 0
        c = 0
        i = 0
        music.unpause()
#……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
    #测试
    if g == 1:
        zhu()
        if d3 == 0:
            d3 = time.time()
        #失败
        if diji.y > 550:
            music.pause()
            buhege.pos = (600,300)
            sounds.shibai.play()
            g = 0
            h = 1
            d2 = time.time()
            d3 = 0
            i = 0
        #结束、计时开始
        if zidan.colliderect(diji):
            music.pause()
            diji.image = "baozha.png"
            if time.time()-d3 <= 7:
                youxiu.pos = (600,300)
                sounds.shengli.play()
            elif time.time()-d3 <= 10:
                lianghao.pos = (600,300)
                sounds.shengli.play()
            elif time.time()-d3 <= 15:
                hege.pos = (600,300)
                sounds.shengli.play()
            elif time.time()-d3 > 15:
                buhege.pos = (600,300)
                sounds.shibai.play()
            d2 = time.time()
            g = 0
            h = 1
            i = 0
            d3 = 0
    #测试完成
    if time.time()-d2 >= 3 and h == 1:
        youxiu.pos = (-999,-999)
        lianghao.pos = (-999,-999)
        hege.pos = (-999,-999)
        buhege.pos = (-999,-999)
        diji.image = "diji.png"
        kaishi.pos = (600,300)
        ceshi.pos = (600,200)
        shibai.pos = (-999,-999)
        it.pos = (1140,100)
        feiji.pos = (-999,999)
        diji.pos = (-999,-999)
        zidan.pos = (-600,-500)
        win.pos = (-999,-999)
        biaoti.pos = (600,50)
        phb.pos = (1140,230)
        h = 0
        music.unpause()
        

go()
